package com.example.guiguihomemedical.service.impl;

import com.example.guiguihomemedical.entity.FollowUp;
import com.example.guiguihomemedical.entity.followup.FollowupHypertension;
import com.example.guiguihomemedical.entity.followup.Followupnewchild;
import com.example.guiguihomemedical.entity.followup.FollowupWith;
import com.example.guiguihomemedical.entity.followup.ListFollowupWith;
import com.example.guiguihomemedical.mapper.FollowUpMapper;
import com.example.guiguihomemedical.service.FollowUpService;
import com.example.guiguihomemedical.utils.DateFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class FollowUpServiceImpl implements FollowUpService {
    @Autowired
    private FollowUpMapper followUpMapper;

    @Override
    public String add(FollowUp followUp, Object object) throws JsonProcessingException {
        int id = -1;
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(object);
        System.out.println(" String json = objectMapper.writeValueAsString(object);：  " + json);

        if (followUp.getFollowUpType().equals("新生儿")) {
            if ( followUpMapper.checkNewChildeIsExist(followUp.getIdCard())>0){//已存在新生儿 的正式随访
                return "已存在该用户的新生儿正式随访，无需在创建！";
            }else {
                Followupnewchild followupnewchild = objectMapper.readValue(json, Followupnewchild.class);
                followUpMapper.insertFollowupnewchild(followupnewchild);
                id = followupnewchild.getId();
            }

        }else if (followUp.getFollowUpType().equals("高血压")){

            FollowupHypertension followupHypertension =objectMapper.readValue(json, FollowupHypertension.class);
            followUpMapper.insertFollowupHypertension(followupHypertension);
            id=followupHypertension.getId();

        }
        String s = objectMapper.writeValueAsString(followUp);

        System.out.println("查看医生id：   " + s);
        if (id != -1) {
            followUp.setFollowUpTypeId(id);
            followUpMapper.insert(followUp);
            return "创建成功";
        } else return "创建失败";
    }

    @Override
    public String update(FollowUp followUp, Object object) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(object);
       if ( followUp.getState()==1&&followUpMapper.select(followUp.getId()).getState()==0){
           if ( followUpMapper.checkNewChildeIsExist(followUp.getIdCard())>0){
               return "已存在该用户的新生儿正式随访，最多只能存在一个新生儿正式随访";
           }
       }

        System.out.println(json);
        followUpMapper.update(followUp);

        if (followUp.getFollowUpType().equals("新生儿")) {
            Followupnewchild followupnewchild = objectMapper.readValue(json, Followupnewchild.class);
            followUpMapper.updateFollowupnewchild(followupnewchild);
        }else if (followUp.getFollowUpType().equals("高血压")){
            FollowupHypertension hypertension = objectMapper.readValue(json, FollowupHypertension.class);


            followUpMapper.updateFollowupHypertension(hypertension);
        }
        return "更新完毕";
    }

    @Override
    public String delete(FollowUp followUp, Object object) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(object);
        FollowUp existOne = followUpMapper.select(followUp.getId());
        if (!followUp.getState().equals(2)){
            return "不可删除！";
        }

        if (existOne == null) {
            return "删除失败，无记录可删除";
        }
        if (followUp.getFollowUpType().equals("新生儿")) {
            Followupnewchild followupnewchild = objectMapper.readValue(json, Followupnewchild.class);
            followUpMapper.deleteFollowupnewchild(followupnewchild);
        }else if (followUp.getFollowUpType().equals("高血压")){
            FollowupHypertension hypertension=objectMapper.readValue(json, FollowupHypertension.class);
            followUpMapper.deleteFollowupHypertension(hypertension);
        }
        followUpMapper.delete(followUp);
        return "已删除随访记录";
    }

    @Override
    public List<ListFollowupWith> findByPage(Integer PageBegin, Integer PageSize, String timeLimit, String dateA, String dateB, Integer state, String type
            , String searchString) throws ParseException {
        //___ 初始化时间条件
        Date date;
        Date date1;

        if (timeLimit == null) {
            date = null;
            date1 = null;
        } else if (timeLimit.equals("最近三个月")) {
            date = new Date();
            date1 = new Date();
            date = DateFormat.addAndSubtractDaysByGetTime(date, 90);
            date1 = DateFormat.addAndSubtractDaysByGetTime(date1, -90);
        } else if (timeLimit.equals("最近半年")) {
            date = new Date();
            date1 = new Date();
            date = DateFormat.addAndSubtractDaysByGetTime(date, 180);
            date1 = DateFormat.addAndSubtractDaysByGetTime(date1, -180);
        } else if (timeLimit.equals("最近一年")) {
            date = new Date();
            date1 = new Date();
            date = DateFormat.addAndSubtractDaysByGetTime(date, 360);
            date1 = DateFormat.addAndSubtractDaysByGetTime(date1, -360);
        } else if (timeLimit.equals("自定义")) {//全部时间
            if (dateA != null && dateA.length() > 0) {
                date = new SimpleDateFormat("yyyy年MM月dd日").parse(dateA);
            } else date = null;

            if (dateB != null && dateB.length() > 0) {
                date1 = new SimpleDateFormat("yyyy年MM月dd日").parse(dateB);
            } else date1 = null;
        } else {
            date = null;
            date1 = null;
        }
        //___ 初始化时间条件

        //___ 预置化分页
        if (PageBegin == null) {
            PageBegin = 0;
        }
        if (PageSize == null) {
            PageSize = 10;
        }
        //___ 预置化分页

        List<FollowUp> followUpList = followUpMapper.findAndSearch(PageBegin, PageSize, date, date1, state, type, searchString);
        ListFollowupWith listFollowupWith;
        List<ListFollowupWith> lL = new ArrayList<>();

        for (FollowUp followUp : followUpList) {
            listFollowupWith = new ListFollowupWith();
            listFollowupWith.setFollowUp(followUp);
            Map<String, Object> objectMap = followUpMapper.findRecordByIdcard(followUp.getIdCard());

            listFollowupWith.setPatientName((String) objectMap.get("name"));
            listFollowupWith.setPatientPhone((String) objectMap.get("phone"));
            listFollowupWith.setSex((String) objectMap.get("sex"));
            listFollowupWith.setPatientResidence(objectMap.get("residence") + (String) objectMap.get("detailedResidence"));

            objectMap = followUpMapper.findDoctorById(followUp.getDoctorId());
            listFollowupWith.setDoctorName((String) objectMap.get("name"));


            lL.add(listFollowupWith);

            System.out.println("查看所属随访用户信息：" + listFollowupWith.getPatientResidence() + listFollowupWith.getDoctorName());
        }


        return lL;
    }

    @Override
    public FollowupWith getOneFollowup(Integer id) {
        FollowUp followUp = followUpMapper.select(id);
        if (followUp != null) {
            Object object = null;
            if (followUp.getFollowUpType().equals("新生儿")){
                object= followUpMapper.seleteFollowupnewchild(followUp.getFollowUpTypeId());

            }else if(followUp.getFollowUpType().equals("高血压")){
                object= followUpMapper.seleteFollowupHypertension(followUp.getFollowUpTypeId());
            }

            FollowupWith followupWith = new FollowupWith();
            followupWith.setFollow(followUp);

            if (object==null){
                return null;
            }else {
                followupWith.setObject(object);
                return followupWith;
            }


        } else return null;
    }

    @Override
    public List<String> getOnesFollowUpType(String idcard) {

        return followUpMapper.getOnesFollowUpType(idcard);
    }
}
